﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gfinder.Domain.Models;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Gfinder.Data.Repositories
{
    public class Repository<T> where T : ModelBase
    {
        public Repository(IMongoDatabase database)
        {
            Database = database;
            Collection = database.GetCollection<T>(typeof(T).Name);
        }

        protected IMongoDatabase Database { get; private set; }

        protected IMongoCollection<T> Collection { get; private set; }

        public virtual T FindById(Guid id)
        {
            var entityResult = Collection.Find(x => x.Id == id).FirstAsync();
            entityResult.Wait();

            return entityResult.Result;
        }

        public virtual List<T> FindAll()
        {
            var entities = Collection.Find(new BsonDocument()).ToListAsync();
            entities.Wait();

            return entities.Result;
        } 

        public virtual T Insert(T entity)
        {
            Collection.InsertOneAsync(entity);

            return entity;
        }
    }
}
